package com.yaojianfeng.openai.controller;

import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.web.bind.annotation.*;

@RestController
public class OpenAIImageController {

    private final OpenAiImageModel imageModel;

    public OpenAIImageController(OpenAiImageModel imageModel) {

        this.imageModel = imageModel;
    }

    /**
     * 生成图片
     * 需要将图片设置小一点,否则容易接口超时
     *
     * @param prompt
     * @return
     */
    @PostMapping("/image")
    public Object createImage(@RequestBody String prompt) {

        OpenAiImageOptions defaultOptions = new OpenAiImageOptions();
        defaultOptions.setSize("256x256");
        defaultOptions.setN(1);
        defaultOptions.setModel("dall-e-2");
        ImagePrompt imagePrompt = new ImagePrompt(prompt, defaultOptions);

        ImageResponse response = imageModel.call(imagePrompt);

        return response.getResult();
    }

}
